<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function val(pushed, popped) {
      const stack = [];
      while (pushed.length > 0) {
        const num = pushed.shift();
        stack.push(num);
        while (popped.length > 0 && popped[0] === stack[stack.length - 1]) {
          popped.shift();
          stack.pop();
        }
      }
      return stack.length === 0;
    };
    console.log(val([1, 2, 3, 4, 5], [4, 5, 3, 2, 1]));
  </script>
</body>

</html>